'
'  ftp_help.bas
'
'  ftp Helper Rountines
'  By Don Dickinson
'  ddickinson@basicguru.com
'==============================================================================

'
'  Constants
'==============================================================================
%FTP_BLOCK_SIZE = 8192        ' How big are the data chunks sent to ftp server


'
'  Globals
'==============================================================================
Global gLastMsg As String
Global gLastErr As Long


'
'  buffer_status
'  Evaluates the return buffer's first 3 characters
'  and returns them as an integer.                                 NOT EXPORTED
'==============================================================================
Function buffer_status(ByVal sBuffer As String) As Long

   Function = Int(Val(Left$(sBuffer,3)))

End Function

'
'  set_last_status
'  Assigns the global status variables
'  based On the buffer passed                                      NOT EXPORTED
'==============================================================================
Sub set_last_status(buffer As String)

   gLastMsg = Trim$(Mid$(buffer, 4))
   gLastErr = buffer_status(buffer)

End Sub

'
'  open_data_socket
'  Opens a server's data socket.
'  Returns the socket's handle if successful.
'  Returns %INVALID_SOCKET if not.                                 NOT EXPORTED
'==============================================================================
Function open_data_socket(sServer As String, sDataSocket As String) As Long

   Dim hSocket As Long
   Dim iPort As Long

   '- The port is returned in ascii format
   '  after the PASV command is sent. This
   '  routine expects the raw response
   '  to the PASV command be passed in
   '  the sDataSocket parameter.
   '
   iPort = Int(CvWrd( Chr$(Int(Val(Parse$(sDataSocket, ",", 6)))) + _
                Chr$(Int(Val(Parse$(sDataSocket, ",", 5)))) ))

   Err = 0
   hSocket = FreeFile
   Tcp Open Port iPort At sServer As hSocket
   If Err Then
      Function = %INVALID_SOCKET
   Else
      Function = hSocket
   End If

End Function

'
'  get_passive_handle
'  Requests a passive mode data connection and returns
'  the socket handle for the data connection if successful.
'  If not, it returns %INVALID_SOCKET                              NOT EXPORTED
'==============================================================================
Function get_passive_handle(ByVal hSocket As Long, sServer As String) As Long
   
   Dim iStatus as Long
   Dim buffer As String

   Tcp Print hSocket, "PASV"
   Tcp Line hSocket, buffer
   iStatus = buffer_status(buffer)
   if (iStatus < 200) or (iStatus > 299) then
'   If buffer_status(buffer) <> 227 Then
      Function = %INVALID_SOCKET
   Else
      Function = open_data_socket(sServer, Parse$(buffer, Any "()", 2))
   End If
   set_last_status buffer


End Function

'
'  read_data_block
'  Returns %True if successful, %False if there was an error or
'  the transfer is complete.                                       NOT EXPORTED
'==============================================================================
Function read_data_block(ByVal hData As Long, Buffer As String) As Long

   Dim buf As String

   buf = String$(%FTP_BLOCK_SIZE, 0)
   Tcp Recv hData, Len(buf), buf
   buffer = buf
   Function = Len(buffer)

End Function


'
'  write_data_block
'  Writes to the tcp socket.
'  Returns %False if an error occurs,
'  otherwise it returns %True.                                     NOT EXPORTED
'==============================================================================
Function write_data_block(ByVal hData As Long, Buffer As String) As Long

   Tcp Send hData, Buffer
   If Err Then
      Function = %False
   Else
      Function = %True
   End If

End Function

